﻿
Imports System.Math
Imports System.IO

Module Module1
    Public Function ReadLine(ByVal sFile As String, Optional ByVal nLine As Long = 1) As String

        Dim sLines() As String
        Dim oFSO As Object
        Dim oFile As Object

        ' Fehlerbehandlung aktivieren
        On Error GoTo ErrHandler

        ' Verweis auf das FileSystemObject erstellen
        oFSO = CreateObject("Scripting.FileSystemObject")

        ' Existiert die Datei überhaupt?
        If oFSO.FileExists(sFile) Then
            ' Datei öffnen
            oFile = oFSO.OpenTextFile(sFile)

            ' Alles lesen und in Array zerlegen
            sLines = Split(oFile.ReadAll, vbCrLf)

            ' Datei schließen
            oFile.Close()

            Select Case Sign(nLine)
                ' (nLine > 0)
                Case 1
                    ' n-te Zeile von vorne beginnend
                    ReadLine = sLines(nLine - 1)

                    ' (nLine < 0)
                Case -1
                    ' n-te Zeile von hinten beginnend
                    ReadLine = sLines(UBound(sLines) + nLine + 1)
            End Select
        End If

ErrHandler:
        ' Objekte zerstören
        oFile = Nothing
        oFSO = Nothing
    End Function

    Public Sub EditLine(ByVal DateiPfad As String, ByVal NeuerZeilenText As String, ByVal ZeilenNr As Long)

        If File.Exists(DateiPfad) Then
            Dim readerline As New StreamReader(DateiPfad, System.Text.Encoding.GetEncoding("ISO-8859-1"))
            Dim configtext As String = readerline.ReadToEnd
            readerline.Close()

            Dim splitted() As String = Split(configtext, vbCrLf)

            If Not (UBound(splitted) < ZeilenNr) Then
                splitted(ZeilenNr - 1) = NeuerZeilenText

                Dim sw As New StreamWriter(DateiPfad, False, System.Text.Encoding.GetEncoding("ISO-8859-1"))

                For Each zeile In splitted
                    sw.WriteLine(zeile)
                Next

                sw.Flush()
                sw.Close()
                sw.Dispose()
            Else
                Dim sw As New StreamWriter(DateiPfad, False)
                For Each zeile In splitted
                    sw.WriteLine(zeile)
                Next
                For i = UBound(splitted) To ZeilenNr
                    sw.WriteLine("")
                Next
                sw.Close()
                EditLine(DateiPfad, NeuerZeilenText, ZeilenNr)
            End If
        Else
            Dim sw2 As New StreamWriter(DateiPfad, False)
            For i = 1 To 50
                sw2.WriteLine("")
            Next
            sw2.Close()
            EditLine(DateiPfad, NeuerZeilenText, ZeilenNr)
        End If

    End Sub

    Public Sub AddLine(ByVal DateiPfad As String, ByVal NeuerZeilenText As String)

        If File.Exists(DateiPfad) Then
            Dim readerline As New StreamReader(DateiPfad, System.Text.Encoding.GetEncoding("ISO-8859-1"))
            Dim configtext As String = readerline.ReadToEnd
            readerline.Close()

            Dim splitted() As String = Split(configtext, vbCrLf)
            Dim sw As New StreamWriter(DateiPfad, False, System.Text.Encoding.GetEncoding("ISO-8859-1"))

            For Each zeile In splitted
                If Not (zeile = "") Then
                    sw.WriteLine(zeile)
                End If
            Next
            sw.WriteLine(NeuerZeilenText)

            sw.Flush()
            sw.Close()
            sw.Dispose()
        Else
            Dim sw2 As New StreamWriter(DateiPfad, False)
            sw2.WriteLine("")
            sw2.Close()
            AddLine(DateiPfad, NeuerZeilenText)
        End If

    End Sub

    Public Sub HTMLine(ByVal DateiPfad As String)

        If File.Exists(DateiPfad) Then
            Dim readerline As New StreamReader(DateiPfad, System.Text.Encoding.GetEncoding("ISO-8859-1"))
            Dim configtext As String = readerline.ReadToEnd
            readerline.Close()

            configtext = Replace(configtext, "<br />", vbCrLf)
            Dim splitted() As String = Split(configtext, vbCrLf)
            Dim sw As New StreamWriter(DateiPfad, False, System.Text.Encoding.GetEncoding("ISO-8859-1"))

            For Each zeile In splitted
                If (zeile <> "") Or (zeile <> vbCrLf) Then
                    sw.WriteLine(zeile)
                End If
            Next

            sw.Flush()
            sw.Close()
            sw.Dispose()
        End If

    End Sub

    Public Function GetFileNameFromURL(ByVal URL As String) As String
        Try
            Return URL.Substring(URL.LastIndexOf("/") + 1)
        Catch ex As Exception
            Return URL
        End Try
    End Function

End Module
